//
//  CXRootViewController.m
//  Codex
//
//  Created by ChenJie on 23/01/2018.
//  Copyright © 2018 Mist. All rights reserved.
//

#import <MJExtension/MJExtension.h>
#import "CXRootViewController.h"
#import "CXSampleService.h"

@interface CXRootViewController ()

@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) CXSampleService *service;

@end

@implementation CXRootViewController

#pragma mark - Public Methods

#pragma mark - Lifecycle Methods

- (instancetype)init {
    self = [super init];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addComponents]; // 构造主界面
    [self fetchDatas];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    [self layoutComponents]; // 布局主界面
}

#pragma mark - Private Methods

- (void)addComponents {
    [self.view addSubview:self.label];
}

- (void)layoutComponents {
    self.label.frame = CGRectMake(10, 30, 300, 44);
}

- (void)fetchDatas {
    NSString *username = @"hxm@xbjwh.com";
    NSString *encryptedPassword = @"xk8iQ+7r8uooeqDayPGnOg==";
    
    [self.service loginWithUsername:username password:encryptedPassword success:^(id response) {
        NSLog(@"success: %@", [response mj_JSONString]);
    } failure:^(NSError *error) {
        NSLog(@"fail: %@", error);
    }];
}

#pragma mark - Event Response Methods

#pragma mark - Delegate Methods

#pragma mark - Setter Methods

#pragma mark - Getter Methods

- (UILabel *)label {
    if (!_label) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
        label.backgroundColor = [UIColor lightGrayColor];
        label.font = [UIFont systemFontOfSize:14.f];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = NSStringFromClass(self.class);
        _label = label;
    }
    return _label;
}

- (CXSampleService *)service {
    if (!_service) {
        _service = [[CXSampleService alloc] init];
    }
    return _service;
}

#pragma mark - Memory Management Methods

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)dealloc {
    
}

@end
